package com.glsc.ngateway.platform.controller.glres;


import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemAuth;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.glres.GlscSystemResource;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.dto.GlscSystemAuthDto;
import com.glsc.ngateway.platform.service.glres.GlscSystemAuthService;
import com.glsc.ngateway.platform.service.glres.GlscSystemResService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 资源管理-系统用户权限
 */
@RestController
@RequestMapping("/api/res/system/auth")
@Slf4j
public class GlscSystemAuthController {

    @Resource
    private UserCache userCache;

    @Resource
    private GlscSystemAuthService glscSystemAuthService;

    @Resource
    private GlscSystemResService glscSystemResService;

    /**
     * 分页查找
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<GlscSystemAuth>> page(@RequestBody GlscSystemAuthDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getUserName())) {
            param.put("LIKE_userName", dto.getUserName());
        }

        if (Objects.isNull(dto.getSystemId())) {
            PlatformResponse.successData(Page.empty());
        }
        param.put("EQ_systemId", dto.getSystemId());

        //User user = userCache.get(request);
        //param.put("EQ_userId", user.getId());

        Page<GlscSystemAuth> page = glscSystemAuthService.page(dto.getPageNo(), dto.getPageSize(), param);
        return PlatformResponse.successData(page);
    }

    /**
     * 根据id查找
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<GlscSystemAuth> findById(@ApiParam(required = false, value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(glscSystemAuthService.findById(id));
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<?> save(@RequestBody GlscSystemAuth obj) {
        glscSystemAuthService.save(obj);
        return PlatformResponse.success();
    }

    /**
     * 根据地删除
     *
     * @param obj
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody GlscSystemAuth obj) {
        GlscSystemAuth auth = glscSystemAuthService.findById(obj.getId());
        GlscSystemResource system = glscSystemResService.findById(auth.getSystemId());
        Assert.isTrue(!system.getSystemManageOaAccount().equals(auth.getUserOaAccount()), "管理员A成员权限信息不可删除");
        glscSystemAuthService.delete(obj.getId());
        return PlatformResponse.success();
    }


}
